
<!--
All rights Reserved, Designed By www.youyacao.com
@Description:图文列表管理
@author:成都市一颗优雅草科技有限公司
@version V4.2
注意：本前端源码遵循 MIT开源协议本内容仅限于个人参考，禁止用于其他的商业用途
需要商业用途或者定制开发等可访问songshu.youyacao.com   联系QQ:422108995 23625059584
开源gitee地址：https://gitee.com/youyacao/songshu-video-page
 -->


<template>
  <div>
    <div class="crumbs">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>
          <i class="el-icon-lx-cascades"></i> 图文列表
        </el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="container">
      <div class="handle-box">
        <el-button
          type="primary"
          icon="el-icon-delete"
          class="handle-del mr10"
          @click="delAllSelection"
        >批量删除
        </el-button>
        <el-select
          v-model="query.type"
          placeholder="分类"
          class="handle-select mr10"
        >
          <el-option label="图文分类" value=""></el-option>
          <el-option
            key="0"
            label="段子列表"
            value="0"
          ></el-option>
          <el-option
            key="1"
            label="图片列表"
            value="1"
          ></el-option>
        </el-select>
        <el-select v-model="query.state" placeholder="审核状态" class="handle-select mr10">
          <el-option label="审核状态" value=""></el-option>
          <el-option key="0" label="待审核" value="0"></el-option>
          <el-option key="1" label="已通过" value="1"></el-option>
          <el-option key="2" label="未通过" value="2"></el-option>
        </el-select>
        <el-input v-model="query.name" placeholder="标题/内容" class="handle-input input-title  mr10"></el-input>
        <el-input v-model="query.uid" placeholder="用户ID" class="handle-input mr10"></el-input>
        <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
      </div>
      <el-table
        :data="tableData"
        border
        class="table"
        ref="multipleTable"
        header-cell-class-name="table-header"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" align="center"></el-table-column>
        <el-table-column prop="id" label="ID" width="55" align="center"></el-table-column>
        <el-table-column prop="title" label="标题"></el-table-column>
        <el-table-column  :show-overflow-tooltip='true' prop="content" label="内容" align="center"></el-table-column>
        <el-table-column label="发布用户" align="center">
          <template slot-scope="scope">
            {{scope.row.name==null?'未设置':scope.row.name}}
          </template>
        </el-table-column>
      <!--  <el-table-column label="视频链接" align="center">
          <template slot-scope="scope">
            <el-link :href="$utils.formatUrl(scope.row.url)" type="primary" target="_blank">点击查看</el-link>
          </template>
        </el-table-column>-->
        <el-table-column label="图片列表(查看大图)" align="center">
          <template slot-scope="scope" >

            <el-image v-if="getImages(scope.row.images).length>0"
              class="table-td-thumb"
              :src="getImages(scope.row.images)[0]"
              :preview-src-list="getImages(scope.row.images)"
            ></el-image>
           <span v-if="getImages(scope.row.images).length==0">无</span>
          </template>
        </el-table-column>
        <el-table-column prop="create_time" label="发布时间"></el-table-column>
        <el-table-column label="状态" align="center">
          <template slot-scope="scope">
            <el-tooltip class="item" effect="dark" content="点击修改状态" placement="right">
              <el-tag class="pointer" v-if="scope.row.state==0" type="" @click="handleState(scope.$index, scope.row)">待审核</el-tag>
            </el-tooltip>
            <el-tag v-if="scope.row.state==1" type="success">已发布</el-tag>
            <el-tag v-if="scope.row.state==2" type="danger">已拒绝</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="180" align="center">
          <template slot-scope="scope">
            <el-button
              type="text"
              icon="el-icon-edit"
              @click="handleEdit(scope.$index, scope.row)"
            >编辑
            </el-button>
            <el-button
              type="text"
              icon="el-icon-delete"
              class="red"
              @click="handleDelete(scope.$index, scope.row)"
            >删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <el-pagination
          background
          layout="total, prev, pager, next"
          :current-page="query.page"
          :page-size="query.pageSize"
          :total="total"
          @current-change="handlePageChange"
        ></el-pagination>
      </div>
    </div>

    <!-- 编辑弹出框 -->
    <el-dialog title="编辑" :visible.sync="editVisible" width="30%">
      <el-form ref="form" :model="form" label-width="70px">
        <el-form-item label="标题">
          <el-input v-model="form.title"></el-input>
        </el-form-item>
        <el-form-item label="内容">
          <el-textarea v-model="form.content"></el-textarea>
        </el-form-item>
        <el-form-item label="用户ID">
          <el-input v-model="form.uid"></el-input>
        </el-form-item>
        <el-form-item label="预览图">
          <el-input v-model="form.images"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveEdit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
    export default {
        name: 'basetable',
        data () {
            return {
                query: {
                    name: '',
                    page: 1,
                    type: '',
                    state: '',
                    uid: '',
                    pageSize: 10
                },
                tableData: [],
                multipleSelection: [],
                delList: [],
                editVisible: false,
                total: 0,
                form: {},
                idx: -1,
                id: -1,
                type_1: ''
            }
        },
        created () {
            this.getData()
        },
        methods: {
            getImages(imgUrl){
                if(!imgUrl){
                    return [];
                }
              let urls = imgUrl.split(",");
              var urlarr = [];
              urls.map((url,key)=>{
                  urlarr.push(this.$utils.formatUrl(url));
              })
                return urlarr;
            },
            // 获取 easy-mock 的模拟数据
            getData () {
                this.$api.textimage.list(this.query).then(res => {
                    this.tableData = res.data
                    this.total = res.total || 0
                })
            },
            // 触发搜索按钮
            handleSearch () {
                this.$set(this.query, 'page', 1)
                this.getData()
            },
            // 删除操作
            handleDelete (index, row) {
                // 二次确认删除
                this.$confirm('确定要删除吗？', '提示', {
                    type: 'warning'
                })
                        .then(() => {
                            var arr = [row.id]
                            this.$api.textimage.delete({ids: arr}).then(res => {
                                if (res.code == 1) {
                                    this.$message.error('删除失败,' + res.msg)
                                    return
                                }
                                this.getData()
                                this.$message.success('删除成功')
                            })
                        })
                        .catch(() => {
                        })
            },
            // 多选操作
            handleSelectionChange (val) {
                this.multipleSelection = val
            },
            delAllSelection () {
                const length = this.multipleSelection.length
                var arr = []
                this.delList = this.delList.concat(this.multipleSelection)
                for (let i = 0; i < length; i++) {
                    arr.push(this.multipleSelection[i].id)
                }
                this.$confirm('确定要删除吗？', '提示', {
                    type: 'warning'
                })
                        .then(() => {
                            this.$api.textimage.delete({ids: arr}).then(res => {
                                if (res.code == 1) {
                                    this.$message.error('删除失败,' + res.msg)
                                    return
                                }
                                this.getData()
                                this.$message.success('删除成功')
                            })
                        })
                        .catch(() => {
                        })
                this.multipleSelection = []
            },
            // 编辑操作
            handleEdit (index, row) {
                this.idx = index
                this.form = row
                this.editVisible = true
            },// 审核操作
            handleState (index, row) {
                this.$confirm('是否通过审核?', '提示', {
                    confirmButtonText: '是',
                    cancelButtonText: '否',
                    type: 'warning'
                }).then(() => {
                    let temp_row = row
                    temp_row.state = 1
                    this.$api.textimage.update(temp_row).then(res => {
                        if (res.code == 1) {
                            this.$message.error(`修改失败,` + res.msg)
                            return
                        }
                        this.editVisible = false
                        this.$message.success(`修改成功`)
                        this.getData()
                    })
                }).catch(() => {
                    let temp_row = row
                    temp_row.state = 2
                    this.$api.video.update(temp_row).then(res => {
                        if (res.code == 1) {
                            this.$message.error(`修改失败,` + res.msg)
                            return
                        }
                        this.editVisible = false
                        this.$message.success(`修改成功`)
                        this.getData()
                    })
                })
            },
            // 保存编辑
            saveEdit () {
                this.$api.textimage.update(this.form).then(res => {
                    if (res.code == 1) {
                        this.$message.error(`修改失败,` + res.msg)
                        return
                    }
                    this.editVisible = false
                    this.$message.success(`修改成功`)
                    this.$set(this.tableData, this.idx, this.form)
                })
            },
            // 分页导航
            handlePageChange (val) {
                this.$set(this.query, 'page', val)
                this.getData()
            },

        }
    }
</script>

<style scoped>
  .pointer {
    cursor: pointer;
  }

  .handle-box {
    margin-bottom: 20px;
  }

  .handle-select {
    width: 120px;
  }

  .handle-input {
    width: 100px;
    display: inline-block;
  }

  .input-title {
    width: 200px;
  }

  .table {
    width: 100%;
    font-size: 14px;
  }

  .red {
    color: #ff0000;
  }

  .mr10 {
    margin-right: 10px;
  }

  .table-td-thumb {
    display: inline-block;
    margin: auto;
    height: 60px;
    width: 80px;
  }
</style>
